/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadastroDeLaboratório.java
 *
 * Created on 31/05/2011, 18:38:28
 */
package Formularios;

import DAO.LaboratorioDAO;
import Logica.Laboratorio;
import java.sql.SQLException;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Claudio
 */
public class CadastroDeLaboratório extends javax.swing.JFrame {

    List<Laboratorio> laboratorios;
    ListSelectionModel lsmLaboratorio;
    DefaultTableModel tmLaboratorio = new DefaultTableModel(null, new String[]{"id", "tipo", "predio", "andar", "Num.Lugares", "Num.Bancadas", "situação"});

    /** Creates new form CadastroDeLaboratório */
    public CadastroDeLaboratório() {
        initComponents();
        setLocationRelativeTo(null);
        desabilitaDados();
        jBAlterar.setEnabled(false);
        jBSalvar.setEnabled(false);
        jBRemover.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jBNovo = new javax.swing.JButton();
        jBSalvar = new javax.swing.JButton();
        jBAlterar = new javax.swing.JButton();
        jBRemover = new javax.swing.JButton();
        jBFechar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jBPesquisar = new javax.swing.JButton();
        jTPesquisar = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTId = new javax.swing.JTextField();
        jTNumBancadas = new javax.swing.JTextField();
        jTNumLugares = new javax.swing.JTextField();
        jTPredio = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTAndar = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jCTipo = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jCSituacao = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTTabela = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuSala = new javax.swing.JMenuItem();
        jMenuLaboratorio = new javax.swing.JMenuItem();
        jMenuEquipamento = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jMenu6 = new javax.swing.JMenu();
        jMenu5 = new javax.swing.JMenu();
        jMenuSair = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Cadastro de Laboratório");

        jPanel1.setBackground(new java.awt.Color(153, 255, 153));

        jBNovo.setText("Novo");
        jBNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBNovoActionPerformed(evt);
            }
        });

        jBSalvar.setText("Gravar");
        jBSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBSalvarActionPerformed(evt);
            }
        });

        jBAlterar.setText("Alterar");
        jBAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAlterarActionPerformed(evt);
            }
        });

        jBRemover.setText("Remover");
        jBRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBRemoverActionPerformed(evt);
            }
        });

        jBFechar.setText("Fechar");
        jBFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBFecharActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jBNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 97, Short.MAX_VALUE)
                .addComponent(jBSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBAlterar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBRemover, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(80, 80, 80)
                .addComponent(jBFechar, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBFechar, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
                    .addComponent(jBRemover, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
                    .addComponent(jBAlterar, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
                    .addComponent(jBSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel2.setBackground(new java.awt.Color(255, 153, 153));

        jBPesquisar.setText("Pesquisar");
        jBPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBPesquisarActionPerformed(evt);
            }
        });

        jTPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTPesquisarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTPesquisar, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBPesquisar)
                .addGap(356, 356, 356))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBPesquisar))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Cadastro de Laboratório", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 13))); // NOI18N

        jLabel1.setText("Código:");

        jLabel2.setText("Número de bancadas:");

        jLabel3.setText("Número de lugares: ");

        jLabel4.setText("Prédio:");

        jTId.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTIdActionPerformed(evt);
            }
        });

        jLabel5.setText("Andar:");

        jLabel6.setText("Tipo:");

        jCTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Informática", "Química", "Eletrônica", "Física" }));

        jLabel7.setText("Situação: ");

        jCSituacao.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Disponível", "Alugado" }));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(301, 301, 301))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTId, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
                            .addComponent(jTNumLugares, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTNumBancadas, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE))
                        .addGap(72, 72, 72)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel4)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(1, 1, 1)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6))))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTPredio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                            .addGap(1, 1, 1)
                            .addComponent(jCSituacao, 0, 151, Short.MAX_VALUE))
                        .addComponent(jCTipo, 0, 152, Short.MAX_VALUE)
                        .addComponent(jTAndar)))
                .addContainerGap(81, Short.MAX_VALUE))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jTAndar, jTId, jTNumBancadas, jTNumLugares, jTPredio});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jTPredio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(37, 37, 37)
                        .addComponent(jTAndar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jCTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                        .addComponent(jCSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4)
                            .addComponent(jTId, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(37, 37, 37)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTNumBancadas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(38, 38, 38)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3)
                                    .addComponent(jTNumLugares, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(jLabel6)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                        .addComponent(jLabel7)
                        .addGap(20, 20, 20))))
        );

        jTTabela.setModel(tmLaboratorio);
        jTTabela.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        lsmLaboratorio = jTTabela.getSelectionModel();
        lsmLaboratorio.addListSelectionListener(new ListSelectionListener() { 
            public void valueChanged(ListSelectionEvent e) { 
                if (! e.getValueIsAdjusting()){ 
                    jTTabelaLinhaSelecionada(jTTabela); 
                } 
            }
        });
        jScrollPane1.setViewportView(jTTabela);

        jMenu1.setText("Cadastro");

        jMenuSala.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuSalaActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuSala);

        jMenuLaboratorio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuLaboratorioActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuLaboratorio);

        jMenuEquipamento.setText("Equipamentos");
        jMenuEquipamento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuEquipamentoActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuEquipamento);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Reservas");
        jMenuBar1.add(jMenu2);

        jMenu3.setText("Pesquisar");
        jMenuBar1.add(jMenu3);

        jMenu6.setText("Locação");
        jMenuBar1.add(jMenu6);

        jMenu5.setText("Relatórios");
        jMenuBar1.add(jMenu5);

        jMenuSair.setText("Sair");
        jMenuSair.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenuSairMouseClicked(evt);
            }
        });
        jMenuSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuSairActionPerformed(evt);
            }
        });
        jMenuBar1.add(jMenuSair);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 653, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(17, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBNovoActionPerformed
        habilitaDados();
        jBSalvar.setEnabled(true);
        jTId.setText("");
        jTAndar.setText("");
        jTNumLugares.setText("");
        jTPredio.setText("");
        jTNumBancadas.setText("");
}//GEN-LAST:event_jBNovoActionPerformed

    private void jTPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTPesquisarActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jTPesquisarActionPerformed

    private void jTIdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTIdActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jTIdActionPerformed

    private void jBPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBPesquisarActionPerformed
        try {
           listarTodosLaboratorios();
           listarLaboratorio();
            //listarSala();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "problemas no campo pesquisa" + ex);
        }
        desabilitaDados();
    }//GEN-LAST:event_jBPesquisarActionPerformed

    private void jBSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBSalvarActionPerformed
         if (verificaDados()) {
            cadastro();
            try {
               listarTodosLaboratorios();

            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "problemas no campo salvar" + ex);
            }
            JOptionPane.showMessageDialog(null, "Laboratório salvo com sucesso!!");
            desabilitaDados();
            jBAlterar.setEnabled(false);
            jBSalvar.setEnabled(false);
            jBRemover.setEnabled(false);
        }
    }//GEN-LAST:event_jBSalvarActionPerformed

    private void jBAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAlterarActionPerformed
         try {
             alteraLab();
             listarTodosLaboratorios();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "problemas no botao alterar" + ex);

        }
        desabilitaDados();
        jBAlterar.setEnabled(false);
        jBSalvar.setEnabled(false);
        jBRemover.setEnabled(false);
    }//GEN-LAST:event_jBAlterarActionPerformed

    private void jBRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBRemoverActionPerformed
        try {
            excluirLab();
            listarTodosLaboratorios();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "problemas no botao excluir" + ex);
        }
        desabilitaDados();
        jBAlterar.setEnabled(false);
        jBSalvar.setEnabled(false);
        jBRemover.setEnabled(false);
    }//GEN-LAST:event_jBRemoverActionPerformed

    private void jBFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBFecharActionPerformed
        dispose();
    }//GEN-LAST:event_jBFecharActionPerformed

    private void jMenuSalaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuSalaActionPerformed
        dispose();
        new CadastroDeSala().show();
    }//GEN-LAST:event_jMenuSalaActionPerformed

    private void jMenuEquipamentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuEquipamentoActionPerformed
        dispose();
        new CadastroDeEquipamento().show();
    }//GEN-LAST:event_jMenuEquipamentoActionPerformed

    private void jMenuLaboratorioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuLaboratorioActionPerformed
        dispose();
        new CadastroDeLaboratório().show();
    }//GEN-LAST:event_jMenuLaboratorioActionPerformed

    private void jMenuSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuSairActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jMenuSairActionPerformed

    private void jMenuSairMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuSairMouseClicked
        System.exit(0);
    }//GEN-LAST:event_jMenuSairMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CadastroDeLaboratório().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBAlterar;
    private javax.swing.JButton jBFechar;
    private javax.swing.JButton jBNovo;
    private javax.swing.JButton jBPesquisar;
    private javax.swing.JButton jBRemover;
    private javax.swing.JButton jBSalvar;
    private javax.swing.JComboBox jCSituacao;
    private javax.swing.JComboBox jCTipo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuEquipamento;
    private javax.swing.JMenuItem jMenuLaboratorio;
    private javax.swing.JMenu jMenuSair;
    private javax.swing.JMenuItem jMenuSala;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTAndar;
    private javax.swing.JTextField jTId;
    private javax.swing.JTextField jTNumBancadas;
    private javax.swing.JTextField jTNumLugares;
    private javax.swing.JTextField jTPesquisar;
    private javax.swing.JTextField jTPredio;
    private javax.swing.JTable jTTabela;
    // End of variables declaration//GEN-END:variables

   public void habilitaDados() {
        // jTId.setEditable(true);
        jTAndar.setEditable(true);
        jTNumLugares.setEditable(true);
        jTNumBancadas.setEditable(true);
        jTPredio.setEditable(true);
        jCTipo.setEditable(true);
        jCTipo.enable(true);
        jCSituacao.enable(true);


    }

    public void desabilitaDados() {
        jTId.setEditable(false);
        jTAndar.setEditable(false);
        jTNumLugares.setEditable(false);
        jTNumBancadas.setEditable(false);
        jTPredio.setEditable(false);
        jCTipo.enable(false);
        jCSituacao.enable(false);
       

    }

    public void cadastro() {

        try {

            Laboratorio lab = new Laboratorio();
            lab.setAndar(Integer.parseInt(jTAndar.getText()));
            lab.setPredio(jTPredio.getText());
            lab.setNumLugares(Integer.parseInt(jTNumLugares.getText()));
            lab.setNumBancadas(Integer.parseInt(jTNumBancadas.getText()));

            if (jCTipo.getSelectedItem() == "Informática") {
                lab.setTipo("Informática");
            }
            if (jCTipo.getSelectedItem() == "Química") {
                lab.setTipo("Química");
            }
            if (jCTipo.getSelectedItem() == "Eletrônica") {
                lab.setTipo("Eletrônica");
            }
             if (jCTipo.getSelectedItem() == "Física") {
                lab.setTipo("Física");
            }
            if (jCSituacao.getSelectedItem() == "Disponível") {
                lab.setSituacao("disponivel");
            }
            if (jCSituacao.getSelectedItem() == "Alugado") {
                lab.setSituacao("alugado");
            }


            LaboratorioDAO dao = new LaboratorioDAO();
            dao.adiciona(lab);

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "problemas no cadastro!!" + ex);
        }
    }

    public void listarLaboratorio() throws SQLException {

        if(jTPesquisar.getText().equals("")){
            JOptionPane.showMessageDialog(null, "digite um ID de um laboratório para pesquisar");
        }else{
            LaboratorioDAO dao = new LaboratorioDAO();
            laboratorios = dao.getLista(Integer.parseInt(jTPesquisar.getText()));
            mostraPesquisa(laboratorios);
        }
    }
      public void listarTodosLaboratorios() throws SQLException {

        LaboratorioDAO dao = new LaboratorioDAO();
        laboratorios = dao.getListaLabs();
        mostraPesquisa(laboratorios);
    }

    public boolean verificaDados() {

        if (!jTNumLugares.getText().equals("") && !jTAndar.getText().equals("") && !jTPredio.getText().equals("") && !jTNumBancadas.getText().equals("")) {
            return true;
        } else {
            JOptionPane.showMessageDialog(null, "os campos Numero de lugares, Andar, tipo ou Numero de Bancadas não podem ser nulos!!");
            return false;
        }
    }

    private void alteraLab() throws SQLException {

        if (jTTabela.getSelectedRow() != -1) {
            if (verificaDados()) {
                Laboratorio lab = new Laboratorio();
                LaboratorioDAO dao = new LaboratorioDAO();

                lab.setId(Integer.parseInt(jTId.getText()));
                lab.setAndar(Integer.parseInt(jTAndar.getText()));
                lab.setNumLugares(Integer.parseInt(jTNumLugares.getText()));
                lab.setNumBancadas(Integer.parseInt(jTNumLugares.getText()));
                lab.setPredio(jTPredio.getText());

                if (jCTipo.getSelectedItem() == "Informática") {
                    lab.setTipo("Informática");
                }
                if (jCTipo.getSelectedItem() == "Química") {
                    lab.setTipo("Química");
                }
                if (jCTipo.getSelectedItem() == "Eletrônica") {
                    lab.setTipo("Eletrônica");
                }
                if (jCTipo.getSelectedItem() == "Física") {
                    lab.setTipo("Física");
                }
                if (jCSituacao.getSelectedItem() == "Disponível") {
                    lab.setSituacao("disponivel");
                }
                if (jCSituacao.getSelectedItem() == "Alugado") {
                    lab.setSituacao("alugado");
                }


                dao.altera(lab);
                JOptionPane.showMessageDialog(null, "Laboratorio alterado com sucesso!");
                desabilitaDados();

            }
        }
    }

    public void excluirLab() throws SQLException {
        int resp = JOptionPane.showConfirmDialog(this, "deseja realmente excluir este Laboratório", "confirmação: ", JOptionPane.YES_NO_OPTION);
        if (resp == JOptionPane.YES_NO_OPTION) { //por default, a resposta é sim!!
           LaboratorioDAO dao = new LaboratorioDAO();
            dao.remove(laboratorios.get(jTTabela.getSelectedRow()));
            mostraPesquisa(laboratorios);

        }

    }

    private void jTTabelaLinhaSelecionada(JTable tabela) {

        if (jTTabela.getSelectedRow() != -1) { //"-1" é o cod do erro quando se da quando exclui um contato
            habilitaDados();
            jBNovo.setEnabled(true);
            jBAlterar.setEnabled(true);
            jBSalvar.setEnabled(false);
            jBRemover.setEnabled(true);

            jTId.setText(String.valueOf(laboratorios.get(tabela.getSelectedRow()).getId()));
            jTAndar.setText(String.valueOf(laboratorios.get(tabela.getSelectedRow()).getAndar()));
            jTNumLugares.setText(String.valueOf(laboratorios.get(tabela.getSelectedRow()).getNumLugares()));
            jTNumBancadas.setText(String.valueOf(laboratorios.get(tabela.getSelectedRow()).getNumBancadas()));
            jTPredio.setText(laboratorios.get(tabela.getSelectedRow()).getPredio());
            jCTipo.setSelectedItem(String.valueOf(laboratorios.get(tabela.getSelectedRow()).getTipo()));
            jCSituacao.setSelectedItem(String.valueOf(laboratorios.get(tabela.getSelectedRow()).getSituacao()));

        } else {
            jTId.setText("");
            jTAndar.setText("");
            jTNumLugares.setText("");
            jTNumBancadas.setText("");
            jTPredio.setText("");

        }

    }

    private void mostraPesquisa(List<Laboratorio> laboratorios) {

        while (tmLaboratorio.getRowCount() > 0) { //vai pegar todas as linha selecionadas maiores que zero, é vai remove-las!
            tmLaboratorio.removeRow(0);
        }
        if (laboratorios.isEmpty()) { //se contatos for vazio, ou seja, naum haver nehum cadastrado...
            JOptionPane.showMessageDialog(null, "nehum Laboratório cadastrado");
        } else {
            String[] linha = new String[]{null, null, null, null, null, null, null}; //colocando null em todas as colunas
            for (int i = 0; i < laboratorios.size(); i++) {
                tmLaboratorio.addRow(linha);
                tmLaboratorio.setValueAt(laboratorios.get(i).getId(), i, 0);
                tmLaboratorio.setValueAt(laboratorios.get(i).getTipo(), i, 1);
                tmLaboratorio.setValueAt(laboratorios.get(i).getPredio(), i, 2);
                tmLaboratorio.setValueAt(laboratorios.get(i).getAndar(), i, 3);
                tmLaboratorio.setValueAt(laboratorios.get(i).getNumLugares(), i, 4);
                tmLaboratorio.setValueAt(laboratorios.get(i).getNumBancadas(), i, 5);
                tmLaboratorio.setValueAt(laboratorios.get(i).getSituacao(), i, 6);
            }
        }
    }
}
